﻿using System.Web;
using System.Web.Security;
using Chowpoo.Util;
using CourseSystem.Common.Models;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Enum;

namespace CourseSystem.Common.Helpers
{
    public class UserHelper
    {
        private const string SessionName = "courseUser";

        public static UserModel LoginUser
        {
            get
            {
                var user = HttpContext.Current.Session[SessionName] as UserModel;
                if (user != null)
                {
                    return user;
                }
                var userIden = HttpContext.Current.User.Identity.Name;
                if (!string.IsNullOrEmpty(userIden))
                {
                    var userIdenArry = userIden.Split('_');
                    if (userIdenArry.Length == 2)
                    {
                        var type = (UserTypeEnum)userIdenArry[1].CheckByte(255);
                        var id = userIdenArry[0].CheckInt(0);
                        if (id == 0)
                        {
                            HttpContext.Current.Response.Redirect(@"~/Account/LogOn");
                            return null;
                        }
                        switch (type)
                        {
                            //TODO:测试学生也可cookie登录,正式上线后将注释掉此段代码!
                            case UserTypeEnum.Student:
                                var stu = StudentBLL.Get(id);
                                if (stu != null)
                                {
                                    user = new UserModel { Id = stu.Id, UserType = UserTypeEnum.Student, UserName = stu.StuName };
                                    SetLogined(user);
                                    return user;
                                } break;
                            case UserTypeEnum.Teacher:
                            case UserTypeEnum.Admin:
                                var tea = TeacherBLL.Get(id);
                                if (tea != null)
                                {
                                    user = new UserModel { Id = tea.Id, UserType = tea.IsAdmin ? UserTypeEnum.Admin : UserTypeEnum.Teacher, UserName = tea.Name };
                                    SetLogined(user);
                                    return user;
                                } break;
                            default:
                                HttpContext.Current.Response.Redirect(@"~/Account/LogOn");
                                return null;
                        }
                    }
                }
                HttpContext.Current.Response.Redirect(@"~/Account/LogOn");
                return null;
            }
        }


        public static void SetLogined(UserModel info)
        {
            HttpContext.Current.Session[SessionName] = info;
            FormsAuthentication.SetAuthCookie(info.Id.ToString() + "_" + (byte)info.UserType, true);
        }

        public static void LogOut()
        {
            HttpContext.Current.Session.RemoveAll();
            // FormsAuthentication.SignOut();
        }
    }
}